package com.crm.custinfo.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.crm.custinfo.domain.HitPackage;
import com.crm.custinfo.mapper.HitPackageMapper;
import com.crm.custinfo.service.IHitPackageService;
import com.ruoyi.common.utils.http.HttpPostUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

/**
 * 全量撞库包Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-19
 */
@Service
public class HitPackageServiceImpl implements IHitPackageService {
    public static final Logger log = LoggerFactory.getLogger(HitPackageServiceImpl.class);
    @Autowired
    private HitPackageMapper hitPackageMapper;

    /**
     * 查询全量撞库包
     *
     * @param id 全量撞库包主键
     * @return 全量撞库包
     */
    @Override
    public HitPackage selectHitPackageById(Long id) {
        return hitPackageMapper.selectHitPackageById(id);
    }

    /**
     * 查询全量撞库包列表
     *
     * @param hitPackage 全量撞库包
     * @return 全量撞库包
     */
    @Override
    public List<HitPackage> selectHitPackageList(HitPackage hitPackage) {
        return hitPackageMapper.selectHitPackageList(hitPackage);
    }
    @Override
    public List<Map<String,Object>> selectHitPackageMd5(){
        return hitPackageMapper.selectHitPackageMd5();
    }
    /**
     * 新增全量撞库包
     *
     * @param hitPackage 全量撞库包
     * @return 结果
     */
    @Override
    public int insertHitPackage(HitPackage hitPackage) {
        //异步插入到外分发器，外分发器撞库用
        int i = hitPackageMapper.insertHitPackage(hitPackage);
        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
//            runSendWffData(hitPackage);
            return "异步同步撞库包";
        });
        return i;
    }

    private void runSendWffData(HitPackage hitPackage) {
        // 发送撞库记录到总库
        String url = "https://userapi.csxskjyx.com/api/company/InsHitpackage4Company";
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("mobile_md5", hitPackage.getMobileMd5());
            params.put("mobile_prefix8_md5", hitPackage.getMobilePrefix8Md5());
            params.put("mobile_sha256", hitPackage.getMobileSha256());
            params.put("city", hitPackage.getCity());
            JSONObject secResult = HttpPostUtils.postJson(url, params);

        } catch (Exception e) {
            log.error("runSendWffData异步出错:{}", e.getMessage());
        }
    }

    /**
     * 修改全量撞库包
     *
     * @param hitPackage 全量撞库包
     * @return 结果
     */
    @Override
    public int updateHitPackage(HitPackage hitPackage) {
        return hitPackageMapper.updateHitPackage(hitPackage);
    }

    /**
     * 批量删除全量撞库包
     *
     * @param ids 需要删除的全量撞库包主键
     * @return 结果
     */
    @Override
    public int deleteHitPackageByIds(Long[] ids) {
        return hitPackageMapper.deleteHitPackageByIds(ids);
    }

    /**
     * 删除全量撞库包信息
     *
     * @param id 全量撞库包主键
     * @return 结果
     */
    @Override
    public int deleteHitPackageById(Long id) {
        return hitPackageMapper.deleteHitPackageById(id);
    }
}
